package soccerBet.project.Transaction;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.kie.api.KieServices;
import org.kie.api.event.rule.DebugAgendaEventListener;
import org.kie.api.event.rule.DebugRuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import soccerBet.project.Calculation.AvaliationFunction;
import soccerBet.project.Calculation.Team;
import soccerBet.project.betfair.util.InflatedMarketPrices;
import soccerBet.project.betfair.util.InflatedMarketPrices.InflatedPrice;
import soccerBet.project.betfair.util.InflatedMarketPrices.InflatedRunner;
import soccerBet.project.contracts.RunnerContract;
import soccerBet.project.dao.ApostasDAO;
import soccerBet.project.dao.ClassificacaoDAO;
import soccerBet.project.dao.InfoPrecosDAO;
import soccerBet.project.dao.JogoDAO;
import soccerBet.project.dao.MapeamentoMercadosDAO;
import soccerBet.project.dao.MercadoDAO;
import soccerBet.project.dao.PrecosDAO;
import soccerBet.project.dao.RunnerDAO;
import soccerBet.project.objects.Apostas;
import soccerBet.project.objects.Classificacao;
import soccerBet.project.objects.InfoPrecos;
import soccerBet.project.objects.Jogo;
import soccerBet.project.objects.MapeamentoMercados;
import soccerBet.project.objects.Mercados;
import soccerBet.project.objects.Precos;
import soccerBet.project.objects.Runner;
import soccerBet.project.ruleEngine.ApostaRule;
import soccerBet.project.ruleEngine.EngineStart;
import soccerBet.project.ruleEngine.TempoJogo;

public class BetsTM {

	public MapeamentoMercadosDAO mapMercDao = new MapeamentoMercadosDAO();
	public JogoDAO jogoDao = new JogoDAO();
	public MercadoDAO mercDao = new MercadoDAO();
	public RunnerDAO runDao = new RunnerDAO();
	public PrecosDAO precoDao = new PrecosDAO();
	public ApostasDAO apostasDao = new ApostasDAO();
	public InfoPrecosDAO infoPrecoDao = new InfoPrecosDAO();
	
	
	public BetsTM() {

	}
	
	public void saveBet(Precos preco, ApostaRule rule){
		preco.setPlacedBet(1);
		precoDao.guardarPrecos(preco);
		
		infoPrecoDao.guardarInfoPrecos(preco,rule);
		
		Runner runnerObj = runDao.getRunnerByPreco(preco.getIdPrecos());
		runnerObj.setEstado(1);

		runDao.guardarRunner(runnerObj);
	}
	

	public List<Precos> makeTransaction(int[] marketId, List<RunnerContract> runners,
			Jogo jogo, Mercados mercado) {
		// TODO Auto-generated method stub

		if (runners != null &&  runners.size()>0) {



			List<Apostas> listObj = apostasDao.getApostasByMercado(mercado.getIdMercado());

			

			MapeamentoMercados mapMerc = mapMercDao.getMapeamentoMercadoByMaketAndJogo(marketId[0],jogo.getIdJogo());

			if (mapMerc == null) {
				mapMerc = new MapeamentoMercados();
				mapMerc.setMarketId(marketId[0]);
				jogoDao.getJogo(jogo.getIdJogo());
				mapMerc.setJogo(jogoDao.getJogoObj());
				mapMerc.setMercados(mercDao.getMercado(mercado.getIdMercado()));
				mapMercDao.insertToDB(mapMerc);

			}

			int i = 0;
			List<Precos> priList = new ArrayList<Precos>();
			for (RunnerContract r : runners) {



				soccerBet.project.objects.Runner run = 	runDao.geRunnerBySelectionIdAndMapMercado(r.getSelectionId(), mapMerc.getIdMapMercado());

				if (run == null) {
					run = new soccerBet.project.objects.Runner();
					run.setSelectionId(r.getSelectionId());
					run.setMapeamentoMercados(mapMerc);
					run.setApostas(listObj.get(i));
					runDao.guardarRunner(run);
				}

				Precos pri = new Precos();
				pri.setData(new Date());
				pri.setBack(r.getBack());
				pri.setLay(r.getLay());
				pri.setRunner(run);
				pri.setSelectionId(r.getSelectionId());
				pri.setPlacedBet(0);
				precoDao.guardarPrecos(pri);
				priList.add(pri);
				
				i++;
			}
			
			return priList;
		}
		return null;
		

	}


	public void closedMarket(int[] marketId, Jogo jogo, Mercados mercado) {
		// TODO Auto-generated method stub


			MapeamentoMercadosDAO mapMercDao = new MapeamentoMercadosDAO();
			JogoDAO jogoDao = new JogoDAO();
			MercadoDAO mercDao = new MercadoDAO();



			MapeamentoMercados mapMerc = mapMercDao.getMapeamentoMercadoByMaketAndJogo(marketId[0],
						jogo.getIdJogo());

			if (mapMerc == null) {
				mapMerc = new MapeamentoMercados();
				mapMerc.setMarketId(marketId[0]);
				jogoDao.getJogo(jogo.getIdJogo());
				mapMerc.setJogo(jogoDao.getJogoObj());
				mapMerc.setMercados(mercDao.getMercado(mercado.getIdMercado()));
				mapMerc.setEstado(2);
				mapMercDao.insertToDB(mapMerc);
			} else {
				mapMerc.setEstado(2);
				mapMercDao.updateToDB(mapMerc);
			}

		}

}


